﻿using Kinlo.Entitys;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using UIWindows.Models;
using UIWindows.Models.Homes;
using UIWindows.Models.Interact;

namespace UIWindows.GlobalStatics
{
    /// <summary>
    /// 全局临时变量----作为各个地方过度使用的不保存
    /// </summary>
    [Attributes.Singleton]
    public class TemporaryStatic : Domains.ViewModelBase
    {
        #region 单项通知

        private bool _isRunStatus;
        /// <summary>
        /// 设备是否在运行
        /// </summary>
        public bool IsRunStatus
        {
            get { return _isRunStatus; }
            set { SetProperty(ref _isRunStatus, value); }
        }
        /// <summary>
        /// 厂商名称
        /// </summary>
        public string Mes_AccountDep { get; set; } = string.Empty;//厂商名称
        /// <summary>
        /// 账号
        /// </summary>
        public string Mes_Account { get; set; } = string.Empty;//账号
        /// <summary>
        /// 账户名
        /// </summary>
        public string Mes_AccountName { get; set; } = string.Empty;//账户名
        /// <summary>
        /// 联系人
        /// </summary>
        public string Mes_AccountUser { get; set; } = string.Empty;//联系人
        /// <summary>
        /// 联系方式
        /// </summary>
        public string Mes_AccountTel { get; set; } = string.Empty;//联系方式
        /// <summary>
        /// 分钟产量
        /// </summary>

        public int MinuteProduction = 0;

        public List<ProductionData> EntityCache { get; set; }
        private Dictionary<string, int> _devicestatus;
        public Dictionary<string, int> DeviceStatus { get => _devicestatus; }
        #endregion

        #region 双向绑定
        private Visibility _isExcision = Visibility.Collapsed;

        public Visibility IsExcision
        {
            get { return _isExcision; }
            set { SetProperty(ref _isExcision, value); }
        }

        private bool _notificationRefresh;
        /// <summary>
        /// 通知刷新
        /// </summary>
        public bool NotificationRefresh
        {
            get { return _notificationRefresh; }
            set { SetProperty(ref _notificationRefresh, value); }
        }

        private double _injectionVolume;
        /// <summary>
        /// 注液量标准
        /// </summary>
        public double InjectionVolume
        {
            get { return _injectionVolume; }
            set { SetProperty(ref _injectionVolume, value); }
        }
        private double _injectionVolumeUP;
        /// <summary>
        /// 注液量上限
        /// </summary>
        public double InjectionVolumeUP
        {
            get { return _injectionVolumeUP; }
            set { SetProperty(ref _injectionVolumeUP, value); }
        }
        private double _injectionVolumeLower;
        /// <summary>
        /// 注液量下限
        /// </summary>
        public double InjectionVolumeLower
        {
            get { return _injectionVolumeLower; }
            set { SetProperty(ref _injectionVolumeLower, value); }
        }
        #endregion
        public List<PlcAlarmDataModel> PLCAlarmList { get; set; }
        public bool Is_Mes_Login { get; set; }
        public ObservableCollection<LinkStatusModel> LinkList { get; set; }
        public ObservableCollection<PLCResectionModel> ExcisionList { get; set; }
        public TemporaryStatic()
        {
            EntityCache = new List<ProductionData>();
            _devicestatus = new Dictionary<string, int>();
            LinkList = new ObservableCollection<LinkStatusModel>()
            {
                new LinkStatusModel(){ 
                    ServiceName = "MES状态",
                    Status = false,
                },
                new LinkStatusModel(){ 
                    ServiceName = "上位机状态",
                    Status = false,
                },
                new LinkStatusModel()
                {
                    ServiceName = "设备状态",
                    Status = true,
                },
            };
            ExcisionList = new ObservableCollection<PLCResectionModel>();

            PLCAlarmList = new List<PlcAlarmDataModel>();
            if (!Directory.Exists(".\\PLCAlarm")) { Directory.CreateDirectory(".\\PLCAlarm"); }
            if (File.Exists(".\\PLCAlarm\\Alarm.xls"))
            {
                using (FileStream fs = new FileStream(".\\PLCAlarm\\Alarm.xls", FileMode.Open, FileAccess.Read))
                {
                    HSSFWorkbook workbook = new HSSFWorkbook(fs);
                    ISheet sheet = workbook.GetSheetAt(0);
                    for (int i = 0; i <= sheet.LastRowNum; i++)
                    {
                        IRow row = sheet.GetRow(i);
                        if (row == null)
                        {
                            PLCAlarmList.Add(new PlcAlarmDataModel
                            {
                                Code = "",
                                Message = "未描述"
                            });
                            continue;
                        }
                        //string? code = GetValueType(row.Cells[0]).ToString();

                        string? address= GetValueType(row.Cells[0]).ToString();
                        string? code = GetValueType(row.Cells[1]).ToString();
                       
                        string? msg = row.Cells.Count < 4?"":GetValueType(row.Cells[2])?.ToString();                        
                        string? grade = row.Cells.Count < 4? GetValueType(row.Cells[2])?.ToString():GetValueType(row.Cells[3])?.ToString();                        
                        PLCAlarmList.Add(new PlcAlarmDataModel
                        {
                            Address= address,
                            AlarmGrade = grade,
                            Code = code??"",
                            Message = msg ?? ""
                        });
                    }

                    PLCAlarmList.RemoveAt(0);
                }
            }
        }

        /// <summary>
        /// 获取单元格类型
        /// </summary>
        /// <param name="cell">目标单元格</param>
        /// <returns></returns>
        private static object GetValueType(ICell cell)
        {
            if (cell == null)
                return null;
            switch (cell.CellType)
            {
                case CellType.Blank:
                    return null;
                case CellType.Boolean:
                    return cell.BooleanCellValue;
                case CellType.Numeric:
                    return cell.NumericCellValue;
                case CellType.String:
                    return cell.StringCellValue;
                case CellType.Error:
                    return cell.ErrorCellValue;
                case CellType.Formula:
                default:
                    return "=" + cell.CellFormula;
            }
        }
    }
}
